package com.linewell.controller;

import com.linewell.service.ILetterService;
import com.linewell.utils.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Title: ssm2
 * @description: 留言管理控制器
 * @author: hyuhuang@linewell.com
 * @since:2018/2/18
 */
@Controller
public class LetterController{

    @Autowired
    ILetterService letterService;

    /**
     * @descripition  发送私信
     * @author hyuhuang@linewell.com
     * @date 2018/2/27 10:52
     * @param request, str
     * @return java.lang.String
     */
    @RequestMapping("/sendLetter")
    @ResponseBody
    public String sendLetter(@RequestParam("letter") String letter,HttpServletRequest request){
        int result = letterService.insertLetter(letter,request);
        return result + " ";
    }
    /**
     * @descripition 根据ID 来查询私信数量，以及条数
     * @author hyuhuang@linewell.com
     * @date 2018/5/5
     * @param userId
     * @return com.linewell.utils.Msg
     */
    @RequestMapping(value = "/selectLetter/{id}" ,method = RequestMethod.GET)
    @ResponseBody
    public Msg selectLetter(@PathVariable("id") Long userId){
        return letterService.selectLetter(userId);
    }


    /**
    * @descripition 根据ID 来查询私信数量，以及条数
    * @author hyuhuang@linewell.com
    * @date 2018/5/5
    * @param userId
    * @return com.linewell.utils.Msg
    */
    @RequestMapping(value = "/selectLetters/{id}" ,method = RequestMethod.GET)
    @ResponseBody
    public Msg selectLetters(@PathVariable("id") Long userId){
        return letterService.selectLetters(userId);
    }
    /**
    * @descripition 判断是否已读私信
    * @author hyuhuang@linewell.com
    * @date 2018/5/5
    * @param letterId
    * @return Msg
    */
    @RequestMapping(value = "isRead" ,method = RequestMethod.GET)
    @ResponseBody
    public Msg updateLetterIsRead(@RequestParam("letterId")String letterId){
        return letterService.updateLetterIsRead(letterId);
    }
    /**
     * @Description 通过父id查找其下的回复信息
     * @author hyuhuang@linewell.com
     * @date 2018/4/18
     * @param letterId
     * @return com.linewell.utils.Msg
     */
    @RequestMapping(value = "getLetterById" ,method = RequestMethod.GET)
    @ResponseBody
    public Msg getLetterById(@RequestParam("letterId")String letterId){
        return letterService.getLetterById(letterId);
    }
    /**
    * @descripition 保存回复信息
    * @author hyuhuang@linewell.com
    * @date 2018/5/5
    * @return
    */
    @RequestMapping(value = "insertLetterReply" ,method = RequestMethod.GET)
    @ResponseBody
    public Msg insertLetterReply(@RequestParam("userId")Long userId,@RequestParam("content")String content,
                                 @RequestParam("letterId")String letterId,@RequestParam("writer")Long writer){
        return letterService.insertLetterReply(userId,content,letterId,writer);
    }
    /**
    * @descripition 根据letterId来获取作者名
    * @author hyuhuang@linewell.com
    * @date 2018/5/5
    * @param letterId model
    * @return
    */
    @RequestMapping(value = "messageReply" ,method = RequestMethod.GET)
    public String messageReply(@RequestParam("letterId") String letterId, Model model){
        String writer = letterService.getUserByletterId(letterId);
        model.addAttribute("writer",writer);
        model.addAttribute("letterId",letterId);
        return "messageReply";
    }
    /**
     * @Description 退回意见
     * @author hyuhuang@linewell.com
     * @date 2018/4/21
     * @param
     * @param: content
     * @param: model
     * @return java.lang.Msg
     */
    @RequestMapping(value = "/page/news/opinion" ,method = RequestMethod.GET)
    @ResponseBody
    public Msg opinion(@RequestParam("content") String content, @RequestParam("articleId") Long articleId){
        return letterService.opinion(content,articleId);
    }
    /**
    * @descripition 根据userId来查询系统消息
    * @author hyuhuang@linewell.com
    * @date 2018/5/5
    * @param userId
    * @return
    */
    @RequestMapping(value = "/selectSystemLetter/{id}" ,method = RequestMethod.GET)
    @ResponseBody
    public Msg selectSystemLetter(@PathVariable("id") Long userId){
        return letterService.selectSystemLetter(userId);
    }
    /**
    * @descripition 传递letterId
    * @author hyuhuang@linewell.com
    * @date 2018/5/5
    * @param letterId model
    * @return
    */
    @RequestMapping(value = "/reapply" ,method = RequestMethod.GET)
    public String reapply(@RequestParam("letterId") String letterId,Model model){
        model.addAttribute ("letterId",letterId);
        return "reapply";
    }
    /**
    * @descripition 根据letterId来查询文章
    * @author hyuhuang@linewell.com
    * @date 2018/5/5
    * @param letterId
    * @return
    */
    @RequestMapping(value = "/getArticleByLetterId" ,method = RequestMethod.GET)
    @ResponseBody
    public Msg getArticleByLetterId(@RequestParam("letterId") String letterId){
        return letterService.getArticleByletterId(letterId);
    }

}
